from django.db import models
from django.utils import timezone

from user_app.models import AccountInfo


class Topic(models.Model):
    id = models.AutoField(primary_key=True)
    subject = models.CharField(max_length=200, blank=False)
    content = models.TextField(null=True)

    num_views = models.SmallIntegerField(default=8)
    num_replies = models.SmallIntegerField(default=0)
    num_thumb = models.SmallIntegerField(default=0)

    closed = models.BooleanField(default=False)
    hidden = models.BooleanField(default=False)
    has_imgs = models.BooleanField(default=False)
    has_attachments = models.BooleanField(default=False)
    has_video = models.BooleanField(default=False)
    level = models.SmallIntegerField(default=0, verbose_name="帖子排序级别，0-10，10为最高置顶")
    #category = models.SmallIntegerField(default=0, verbose_name="帖子分类，0默认，1智慧消防，2维保技术，3厂家品牌")
    is_simple = models.BooleanField(default=False,verbose_name="简单格式上传，非富文本格式")

    account_info = models.ForeignKey('user_app.AccountInfo', on_delete=models.DO_NOTHING, related_name="+")
    last_post_account_id = models.ForeignKey('user_app.AccountInfo', on_delete=models.DO_NOTHING, related_name="+", null=True)

    created_on = models.DateTimeField('帖子发布时间', default=timezone.now)
    updated_on = models.DateTimeField('帖子更新时间', default=timezone.now)
    last_reply_on = models.DateTimeField('最近一次回帖时间', default=timezone.now)

    def __str__(self):
        #return self.subject + "---" + self.account_info
        return self.subject


class TopicCategory(models.Model):
    id = models.IntegerField(primary_key=True)
    category = models.CharField(max_length=20, blank=False)
    enable = models.SmallIntegerField(default=0)

    def __str__(self):
        return self.category


class Comment(models.Model):
    id = models.AutoField(primary_key=True)
    topic_id = models.IntegerField(default=0, verbose_name='帖子ID')
    content = models.TextField()
    num_thumb = models.SmallIntegerField(default=0)
    parent_id = models.IntegerField(default=0, verbose_name='父级评论ID')
    reply_account_id = models.IntegerField(default=0, verbose_name='评论回复给某个用户')
    category = models.SmallIntegerField(default=0, verbose_name="话题类型, TopicCategory表外键")
    is_manual = models.SmallIntegerField(default=0, verbose_name="是否为脚本评论，1为脚本")

    created_by_id = models.IntegerField('创建的用户ID', default=0)
    created_on = models.DateTimeField('帖子发布时间', default=timezone.now)


class Attachments(models.Model):
    id = models.AutoField(primary_key=True)
    file_name = models.CharField(max_length=200, blank=False)
    file_url = models.CharField(max_length=200, blank=False, verbose_name="文件OSS完整URL")
    suffix = models.CharField(max_length=20, blank=False)
    num_downloads = models.IntegerField(default=0)

    topic_id = models.IntegerField('附件关联的帖子')
    category = models.SmallIntegerField(default=0, verbose_name="话题类型, TopicCategory表外键")
    created_on = models.DateTimeField('上传时间', default=timezone.now)
    created_by_id = models.IntegerField('附件关联的用户ID')

    def __str__(self):
        return "<QuerySet [<Attachments: ID:%s---%s>" % (str(self.id), self.file_name)


class TopicFollow(models.Model):
    id = models.AutoField(primary_key=True)
    follow_topic_id = models.IntegerField('关注的帖子ID')
    category = models.SmallIntegerField(default=0, verbose_name="话题类型, TopicCategory表外键")
    topic_account_id = models.IntegerField('帖子创建者ID', default=0)

    created_by_id = models.IntegerField('用户ID')
    created_on = models.DateTimeField('创建时间', default=timezone.now)


class TopicThumb(models.Model):
    id = models.AutoField(primary_key=True)
    topic_id = models.IntegerField('帖子ID', default=0)
    category = models.SmallIntegerField(default=0, verbose_name="话题类型, TopicCategory表外键")
    topic_account_id = models.IntegerField('帖子所有者ID', default=0)

    created_by_id = models.IntegerField('用户ID', default=0)
    created_on = models.DateTimeField('创建时间', default=timezone.now)


class CommentThumb(models.Model):
    id = models.AutoField(primary_key=True)
    comment_id = models.IntegerField('评论ID', default=0)
    comment_account_id = models.IntegerField('评论所有者ID', default=0)
    category = models.SmallIntegerField(default=0, verbose_name="话题类型, TopicCategory表外键")

    created_by_id = models.IntegerField('用户ID', default=0)
    created_on = models.DateTimeField('创建时间', default=timezone.now)


class ActivityTopic(models.Model):
    """
        3大话题分类中的'活动'分类
    """
    id = models.AutoField(primary_key=True)
    subject = models.CharField(max_length=200, blank=False)
    start_time = models.DateTimeField('活动开始时间', default=timezone.now)
    end_time = models.DateTimeField('活动开始时间', default=timezone.now)
    banner_image = models.CharField(max_length=100, blank=False, verbose_name="轮播大图URL")
    cover_image = models.CharField(max_length=100, blank=False, verbose_name="活动封面URL")
    content = models.TextField(null=True)

    num_views = models.SmallIntegerField(default=0)
    num_replies = models.SmallIntegerField(default=0)
    num_thumb = models.SmallIntegerField(default=0)

    closed = models.BooleanField(default=False)
    hidden = models.BooleanField(default=False)
    level = models.SmallIntegerField(default=0, verbose_name="帖子排序级别，0-10，10为最高置顶")

    account_info = models.ForeignKey('user_app.AccountInfo', on_delete=models.DO_NOTHING, related_name="+")
    last_post_account_id = models.ForeignKey('user_app.AccountInfo', on_delete=models.DO_NOTHING, related_name="+", null=True)

    created_on = models.DateTimeField('帖子发布时间', default=timezone.now)
    updated_on = models.DateTimeField('帖子更新时间', default=timezone.now)
    last_reply_on = models.DateTimeField('最近一次回帖时间', default=timezone.now)


class BoardTopic(models.Model):
    """
        3大话题分类中的'品牌'分类
    """
    id = models.AutoField(primary_key=True)
    subject = models.CharField(max_length=200, blank=False)
    banner_image = models.CharField(max_length=100, blank=False, verbose_name="轮播大图URL")
    cover_image = models.CharField(max_length=100, blank=False, verbose_name="活动封面URL")
    company_info = models.TextField(null=True, verbose_name="公司简介")
    product_info = models.TextField(null=True, verbose_name="产品简介")
    develop = models.TextField(null=True, verbose_name="发展历程")
    case = models.TextField(null=True, verbose_name="成功案例")

    num_views = models.SmallIntegerField(default=0)
    num_replies = models.SmallIntegerField(default=0)
    num_thumb = models.SmallIntegerField(default=0)

    closed = models.BooleanField(default=False)
    hidden = models.BooleanField(default=False)
    level = models.SmallIntegerField(default=0, verbose_name="帖子排序级别，0-10，10为最高置顶")

    account_info = models.ForeignKey('user_app.AccountInfo', on_delete=models.DO_NOTHING, related_name="+")
    last_post_account_id = models.ForeignKey('user_app.AccountInfo', on_delete=models.DO_NOTHING, related_name="+", null=True)

    check_status = models.SmallIntegerField(default=0, verbose_name="审核状态，0未审核，1已通过，2拒绝")
    check_content = models.CharField(max_length=100, verbose_name="审核补充内容", default='')

    created_on = models.DateTimeField('帖子发布时间', default=timezone.now)
    updated_on = models.DateTimeField('帖子更新时间', default=timezone.now)
    last_reply_on = models.DateTimeField('最近一次回帖时间', default=timezone.now)
